package com.dhecp.project.oa.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.dhecp.common.annotation.Log;
import com.dhecp.common.annotation.RepeatSubmit;
import com.dhecp.common.core.domain.AjaxResult;
import com.dhecp.common.core.page.TableDataInfo;
import com.dhecp.common.enums.BusinessType;
import com.dhecp.core.constant.ModuleTable;
import com.dhecp.core.utils.ExportExcelUtils;
import com.dhecp.project.common.controller.BaseModuleController;
import com.dhecp.project.oa.domain.OfficeStock;
import com.dhecp.project.oa.service.OfficeStockService;
import com.dhecp.project.system.domain.SysDictionaryForExport;
import com.dhecp.project.system.service.SysDictionaryForFieldService;

/**
 * 办公用品库存 控制层
 * @author lwl
 * @version 2020年7月22日
 */
@Controller
@RequestMapping("/oa/officestock")
public class OfficeStockController extends BaseModuleController {

private String moduleTable = ModuleTable.OFFICE_STOCK;
    
    @Autowired
    private OfficeStockService officeStockService;
        
    @Autowired
    private SysDictionaryForFieldService dictionaryForFieldService;
    
    /**
     * 打开查询列表页
     * @param params
     * @return
     * @author DH-Xxj 
     */
	@RequiresPermissions("oa:officestock:view")
    @GetMapping()
    public ModelAndView listPage(@RequestParam Map<String, Object> params){
		params.put("moduleTable", moduleTable);
    	params.put("entity", new OfficeStock());
    	params.put("prefix","oa/oa"); //页面路径
    	
    	ModelAndView mv = this.openListPage(params);
    	mv.addObject("oa", "officestock");
		return mv;
    }
	
	/**
     * 查询所有信息
     * @param params
     * @return TableDataInfo
     * @author lwl
     */
	@GetMapping("/list")
    @RequiresPermissions("oa:officestock:list")
    @ResponseBody
    public TableDataInfo list(@RequestParam Map<String, Object> params)
    {
		params.put("moduleTable", moduleTable);
        List<OfficeStock> list = new ArrayList<OfficeStock>();
        try {
        	list = officeStockService.queryAll(params);
        } catch (Exception e) {
            e.printStackTrace();
            return getDataTable(this.getExceptionMessage(e).toString());            
        }
        return getDataTable(list);
    }
	
	/**
     * 数据导出Excel
     * @param params
     * @return
     * @throws Exception
     * @author DH-Xxj
     */
    @RequiresPermissions("oa:officestock:export")
    @Log(title = "办公用品库存导出", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    @ResponseBody
    @RepeatSubmit
    public AjaxResult export(@RequestParam Map<String, Object> params) throws Exception {
		params.put("moduleTable", moduleTable);
    	params.put("entity", new OfficeStock()); 
        List<OfficeStock> list = officeStockService.queryAll(params);
        params.put("getFieldAttributesType", "export");
    	List<SysDictionaryForExport> fieldList = dictionaryForFieldService.queryExportFieldList(params);
        ExportExcelUtils<OfficeStock> util = new ExportExcelUtils<OfficeStock>(moduleTable,new OfficeStock());
        return util.exportExcel(list, fieldList, "办公用品库存数据", "export");
    }
}
